import { commentGoods, getOrderInfo } from '../../service/order'
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: [],
    value: [],
    order_id: '',
    content: [],
    radio: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const order_id = options.order_id
    console.log(order_id)
    getOrderInfo(order_id, { include: 'orderDetails.goods' }).then(res => {
      console.log(res)
      this.setData({ goods: res.orderDetails.data, order_id })
      console.log(this.data.goods)
    })
  },

  /**
   * 星级评价
   */
  onChange(event) {
    this.data.value[event.target.id] = event.detail
    this.setData({
      value: this.data.value
    });
  },

  /**
   * 获取输入内容
   */
  changeContent(e) {
    console.log(e)
    const id = e.target.id
    this.data.content[id] = e.detail
    this.setData({
      content: this.data.content
    });
    console.log(this.data.content)
  },

  /**
   * 获取评价等级
   */
  changeRank(e) {
    console.log(e)
    this.data.radio[e.target.id] = e.detail
    this.setData({ radio: this.data.radio })
    console.log(this.data.radio)
  },

  /**
   * 提交评价
   */
  toComment() {
    // let pastComment=wx.getStorageSync('commentedList');
    // wx.setStorageSync('commentedList', [...pastComment,this.data.order_id]);
    const order_id = this.data.order_id
    console.log(this.data.goods)
    this.data.goods.forEach((item, index) => {
      let data = { goods_id: '', content: '', rate: '', star: '' }
      data.goods_id = item.goods_id
      data.content = this.data.content[index]
      data.star = this.data.value[index]
      data.rate = this.data.radio[index]
      console.log(data)
      commentGoods(order_id, data).then(() => {
        Toast({
          type: 'success',
          message: '评价成功',
          onClose: () => {
            wx.navigateBack();
          },
        });
      })

    })
  }


})